/*
 * This file is part of freewm.
 *
 * freewm is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * freewm is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with freewm.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "incs.h"

// Function prototypes.
void get_args(int argc, char **argv, int *version, int *help, int *debug);
void show_version(int bye);
void show_help(int bye);

// Program entry point.
int main(int argc, char **argv)
{
	int version = 0, help = 0, debug = 0;
	get_args(argc, argv, &version, &help, &debug);
		
	if (version)
		show_version(1);
	else if (help)
		show_help(1);
	else if (debug)
		logger_enable();
	else if (argc > 1)
		show_help(1);
	
	// Let there be light.
	freewm_start();
	
	return 0;
}

// Interpret the command line arguments.
void get_args(int argc, char **argv, int *version, int *help, int *debug)
{
	int i;
	
	for (i = 1; i < argc; i++)
	{
		// We have a switch.
		if (argv[i][0] == '-')
		{
			// VERSION
			if (!strcmp(argv[i], "--version"))
			{
				*version = 1;
				return;	// We return because if version is specified, we will
						// not be doing anything else.
			}
			// HELP
			else if (!strcmp(argv[i], "--help"))
			{
				*help = 1;
				return;	// Ditto with above.
			}
			// DEBUG
			else if (!strcmp(argv[i], "--debug"))
				*debug = 1;
		}
	}
}

// Show version message.
void show_version(int bye)
{
	printf("freewm %s\n", VERSION);
	
	if (bye)
		exit(0);
}

// Show help message.
void show_help(int bye)
{
	show_version(0);
	
	printf(	"\nOptions:\n"
			"  --version       Show program's version and exit\n"
			"  --help          Show this message and exit\n"
			"  --debug         Enable output of debug information\n");
	
	if (bye)
		exit(0);
}
